package com.wayhome.srpingbootmybatis.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wayhome.srpingbootmybatis.excel.Dr9225AccessInfo;
import com.wayhome.srpingbootmybatis.exception.CustomerException;
import com.wayhome.srpingbootmybatis.policy.dao.PolicyDocDao;
import com.wayhome.srpingbootmybatis.policy.dto.PolicyDocDTO;
import com.wayhome.srpingbootmybatis.policy.dto.PolicyDocExcelDTO;
import com.wayhome.srpingbootmybatis.policy.enums.PolicyTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
public class PolicyDocListener extends AnalysisEventListener<PolicyDocExcelDTO> {

    private PolicyDocDao policyDocDao;

    public PolicyDocListener(PolicyDocDao policyDocDao) {
        this. policyDocDao = policyDocDao;
    }


    public static final String YES = "是";
    public static final String NO = "否";

    String resultMsg = "";

    public String getResultMsg() {
        return resultMsg;
    }
    private int successNum = 0;

    List<String> messageList = new ArrayList<>();

    public List<String> getMessageList() {
        return messageList;
    }

    @Override
    public void invoke(PolicyDocExcelDTO policyDocExcelDTO, AnalysisContext analysisContext) {
        try {
            //检查参数
            checkImportParam(policyDocExcelDTO);

            PolicyDocDTO policyDocDTO = new PolicyDocDTO();
            BeanUtils.copyProperties(policyDocExcelDTO, policyDocDTO);
            //政策类型
            String policyType = PolicyTypeEnum.getCodeByMsg(policyDocExcelDTO.getPolicyTypeName());
            policyDocDTO.setPolicyType(policyType);
            //发布日期
            String pubDateStr = policyDocExcelDTO.getPubDateStr();
            if (!StringUtils.isEmpty(pubDateStr)) {

                Date pubDate = null;
                if (pubDateStr.contains("/")) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    pubDate = simpleDateFormat.parse(pubDateStr);
                }else if (pubDateStr.contains("-")) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    pubDate = simpleDateFormat.parse(pubDateStr);
                }
                policyDocDTO.setPubDate(pubDate);
            }
            //是否补充或调整
            String suptFlag = YES.equals(policyDocExcelDTO.getSuptFlagStr()) ? "1" : "0";
            policyDocDTO.setSuptFlag(suptFlag);
            if (YES.equals(policyDocExcelDTO.getSuptFlagStr()) && StringUtils.isEmpty(policyDocExcelDTO.getSuptDocNo())) {
               throw new CustomerException("当前政策是补充或调整是，对应的文号不能为空");
            }
            //是否在有效期内
            String effectiveFlag = YES.equals(policyDocExcelDTO.getEffectiveFlagStr()) ? "1" : "0";
            policyDocDTO.setEffectiveFlag(effectiveFlag);
            policyDocDTO.setDefaultInsertParam();
            //新增政策
            policyDocDao.insertPolicyDoc(policyDocDTO);
            successNum++;
        } catch (Exception e) {
            String errMsg = "序号为：" + policyDocExcelDTO.getIndex() + "的政策解析时出错，错误信息为：" + e.getMessage();
            log.info("政策文件模版导入时出现错误，{}", errMsg, e);
            messageList.add(errMsg);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("========>政策解析文件完成,新增{}条数据,解析过程中出现{}条数据发生错误", successNum, messageList.size());
        resultMsg = "导入政策文件解析完成，共成功新增" + successNum + "条数据， 解析过程中出现" +messageList.size() +  "条数据发生错误";
        messageList.add(0, resultMsg);
    }

    private void checkImportParam(PolicyDocExcelDTO request) throws Exception {
        if (Objects.isNull(request)) {
            throw new CustomerException("请求参数不能为空");
        }
        if (Objects.isNull(request.getIndex())) {
            throw new CustomerException("序号不能为空");
        }
        if (StringUtils.isEmpty(request.getPolicyTypeName())) {
            throw new CustomerException("类别不能为空");
        }
        if (StringUtils.isEmpty(request.getPolicyYear())) {
            throw new CustomerException("年份不能为空");
        }
        if (StringUtils.isEmpty(request.getDocName())) {
            throw new CustomerException("文件名称不能为空");
        }
        if (Objects.isNull(request.getPubDateStr())) {
            throw new CustomerException("具体发文时间不能为空");
        }
        if (StringUtils.isEmpty(request.getPubDept())) {
            throw new CustomerException("发文单位不能为空");
        }
        if (StringUtils.isEmpty(request.getDocLmtPeriod())) {
            throw new CustomerException("文件有效时限不能为空");
        }
        if (StringUtils.isEmpty(request.getEffectiveFlagStr())) {
            throw new CustomerException("是否在有效期内不能为空");
        }
    }
}
